function charsize(ch)
if not ch then return 0
 elseif ch >=252 then return 6
 elseif ch >= 248 and ch < 252 then return 5
 elseif ch >= 240 and ch < 248 then return 4
 elseif ch >= 224 and ch < 240 then return 3
 elseif ch >= 192 and ch < 224 then return 2
 elseif ch < 192 then return 1
end
end


-- 计算utf8字符串字符数, 各种字符都按一个字符计算
-- 例如utf8len("1你好") => 3
function utf8len(str)
  local len = 0
  local aNum = 0 --字母个数
  local hNum = 0 --汉字个数
  local currentIndex = 1
  while currentIndex <= #str do
    local char = string.byte(str, currentIndex)
    local cs = charsize(char)
    currentIndex = currentIndex + cs
    len = len +1
    if cs == 1 then
      aNum = aNum + 1
     elseif cs >= 2 then
      hNum = hNum + 1
    end
  end
  return len, aNum, hNum
end


-- 截取utf8 字符串
-- str:            要截取的字符串
-- startChar:    开始字符下标,从1开始
-- numChars:    要截取的字符长度
function utf8sub(str, startChar, numChars)
  local startIndex = 1
  while startChar > 1 do
    local char = string.byte(str, startIndex)
    startIndex = startIndex + charsize(char)
    startChar = startChar - 1
  end

  local currentIndex = startIndex

  while numChars > 0 and currentIndex <= #str do
    local char = string.byte(str, currentIndex)
    currentIndex = currentIndex + charsize(char)
    numChars = numChars -1
  end
  return str:sub(startIndex, currentIndex - 1)
end